<?php
require 'includes/config.php';
require 'includes/auth.php';
require 'includes/sanitize.php';
$data = sanitize($_POST);
// Check if user posted some data in the form
if (isset($data['submit'])) {
	if(($data['pw2'] == $data['pw3'])){
		// Tell backend to update the userinfo
		$request = new makeRequest($backend.'api.php', 'POST','method=user&auid='
		.$_SESSION['uid'].'&accesstoken='.$_SESSION['accesstoken'].'&username='
		.$data['username'].'&password='.$data['pw2'].'&mail='.$data['mail'].'&firstname='.$data['firstname'].
		'&lastname='.$data['lastname'].'&location='.$data['location'].
		'&age='.$data['age'].'&description='.$data['description']);
		$tmp = $request->makeRequest();
		$http_code = $tmp[1];
		// Check for errors
		if($http_code == 200){
			$msg = "User profile updated!";
		}else{
			$msg = "Something went wrong, try again later<br /> HTTP error code: ".$http_code;
		}
	}
}
// Get user info to display in form
$request = new makeRequest($backend.'api.php?method=user&id='.$_SESSION['uid'], 'GET');
$tmp = $request->makeRequest();
$rbody = $tmp[0];
$http_code = $tmp[1];
$obj = json_decode($rbody);
// Check for errors 
if($http_code == 200){
	$username = 	$obj[0]->{'username'};
	$mail = 		$obj[0]->{'mail'};
	$firstname = 	$obj[0]->{'firstname'};
	$lastname = 	$obj[0]->{'lastname'};
	$location = 	$obj[0]->{'location'};
	$age = 			$obj[0]->{'age'};
	$description = 	$obj[0]->{'description'};
}

require 'includes/header.php';
require 'includes/topmenu.php';
echo '<div id="kaka">';
require 'includes/sidemenu.php'
?>
<div id="content"><?php
echo $msg;
// Form with pre filled in info
?>
<p>
<table>
	<form method="post" action="<?php echo $PHP_SELF; ?>">
	<tr>
		<td>Username:<br />
		</td>
		<td><input type="text" name="username"
			value="<?php echo $username; ?>" /><br />
		</td>
	</tr>
	<tr>
		<td>Mail: <br />
		</td>
		<td><input type="text" name="mail" value="<?php echo $mail; ?>" /><br />
		</td>
	</tr>
	<tr>
		<td>First name: <br />
		</td>
		<td><input type="text" name="firstname"
			value="<?php echo $firstname; ?>" /><br />
		</td>
	</tr>
	<tr>
		<td>Last name: <br />
		</td>
		<td><input type="text" name="lastname"
			value="<?php echo $lastname; ?>" /><br />
		</td>
	</tr>
	<tr>
		<td>Location: <br />
		</td>
		<td><input type="text" name="location"
			value="<?php echo $location; ?>" /><br />
		</td>
	</tr>
	<tr>
		<td>Age: <br />
		</td>
		<td><input type="text" name="age" value="<?php echo $age; ?>" /><br />
		</td>
	</tr>
	<tr>
		<td>Description: <br />
		</td>
		<td><input type="text" name="description"
			value="<?php echo $description; ?>" /><br />
		</td>
	</tr>
	<tr>
		<td>New password: <br />
		</td>
		<td><input type="password" name="pw2" /><br />
		</td>
	</tr>
	<tr>
		<td>Repeat new password: <br />
		</td>
		<td><input type="password" name="pw3" /><br />
		</td>
	</tr>
	<tr>
		<td><input type="submit" Value="Update" name="submit" />
	
	</form>
	</td>
	<td></td>
	</tr>
</table>
	</p>
<?php

require 'includes/footer.php';
?>